// pages/post-detail/post-detail.js

import { postList } from '../../data/data.js'
// import Notify from '@vant/weapp/notify/notify';
import Notify from '@vant/weapp/notify/notify'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    postData: [],
    collected: false,
    _pid: null,
    _postsCollected: {},
    _mgr: null,
    isPlaying: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options);
    // const postData = postList[options.pid]
    // console.log(postData);
    // const postData = postList.find((item) => {
    //   console.log(item);
    //   item.postId === parseInt(options.pid)
    // })
    // console.log(postData);
    // this.data.postData = postList.filter((item) => {
    //   item.postId == 1
    // })
    // console.log(this.data.postData);
    // this.data._pid = options.pid
    // let postData = []
    // postList.forEach((item) => {
    //   if (item.postId == parseInt(options.pid)) {
    //     postData = item
    //   }
    // })
    // this.setData({
    //   postData
    // })
    // console.log(postData);
    // console.log(this.data.postData);
    console.log(options);
    const postData = postList[options.pid]
    this.data._pid = options.pid
    const postsCollected = wx.getStorageSync('posts_collected')
    // console.log(postsCollected);

    if (postsCollected) {
      this.data._postsCollected = postsCollected
    }

    let collected = postsCollected[this.data._pid]
    // console.log(collected);
    if (collected === undefined) {
      // 如果undefined 说明文章从来没有被收藏过
      collected = false
    }

    const mgr = wx.getBackgroundAudioManager()
    this.data._mgr = mgr
    mgr.onPlay(this.onMusicStart)
    mgr.onPause(this.onMusicStop)
    this.setData({
      collected,
      postData,
      isPlaying: this.currentMusicIsPlaying()
    })
  },

  onCollect(event) {
    const postsCollected = this.data._postsCollected;
    // wx.getStorageSync('key')
    postsCollected[this.data._pid] = !this.data.collected
    // this.data.collected = true
    this.setData({
      collected: !this.data.collected
    })
    // wx.showToast({
    //   title: this.data.collected ? '收藏成功' : '取消收藏',
    //   icon: 'success',
    //   duration: 2000
    // })
    // Notify('通知内容');
    Notify({ type: 'primary', message: this.data.collected ? '收藏成功' : '取消收藏', duration: 2000, });
    // console.log(this.data.collected);
    wx.setStorageSync('posts_collected', postsCollected)

    //模态框
    // wx.showModal({
    //   title: '提示',
    //   content: '这是一个模态弹窗',
    //   success(res) {
    //     if (res.confirm) {
    //       console.log('用户点击确定')
    //     } else if (res.cancel) {
    //       console.log('用户点击取消')
    //     }
    //   }
    // })

  },

  onShare() {
    wx.showActionSheet({
      itemList: ['分享到QQ', '分享到微信', '分享到朋友圈'],
      success(res) {
        console.log(res.tapIndex)
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  //播放
  currentMusicIsPlaying() {
    if (app.gIsPlayingMusic && app.gIsPlayingPostId === this.data._pid) {
      return true
    }
    return false
  },

  onMusicStart() {
    // const mgr = wx.getBackgroundAudioManager()
    // mgr.onPlay()
    const mgr = this.data._mgr
    const music = this.data.postData.music
    console.log(music);
    mgr.src = music.url;
    mgr.title = music.title
    mgr.coverImgUrl = music.coverImg

    app.gIsPlayingMusic = true
    app.gIsPlayingPostId = this.data._pid

    this.setData({
      isPlaying: true,
    })
  },
  //停止
  onMusicStop() {
    // const mgr = wx.getBackgroundAudioManager()
    const mgr = this.data._mgr
    mgr.pause()
    app.gIsPlayingMusic = false
    app.gIsPlayingPostId = -1
    this.setData({
      isPlaying: false
    })
  },
  //重播
  review() {
    // const mgr = this.data._mgr
    // mgr.stop();
    // mgr.play()
    // this.setData({ isPlaying: true });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})